"use strict";

import *  as tpsClass from './../Model/TPS/index';
/** 根据棱镜高差计算断面旋转后水平、竖直分量 */
class PrismSectionCalculate {

    /** 初始断面的中心坐标 */
    public sectionCenter: tpsClass.Coor = null;
   
    /** 初始全站仪（后视pb、前视pf）所在位置坐标 */
    public targetOriginal: tpsClass.Coor = null;
    
    /** 当前全站仪（后视pb、前视pf）所在位置坐标 */
    public targetNow: tpsClass.Coor = null;
    
    /** 初始辅助棱镜坐标*/
    public prismOriginal: tpsClass.Coor = null;
    
    /** 当前辅助棱镜坐标*/
    public prismNow: tpsClass.Coor = null;
    
    constructor(option) {
        this.sectionCenter = option.sectionCenter;
        this.targetOriginal = option.targetOriginal;
        this.targetNow = option.targetNow;
        this.prismOriginal = option.prismOriginal;
        this.prismNow = option.prismNow;
    }
    
    /** 
     * 根据断面旋转后与断面中心水平线的夹角，求取目标点相对于断面中线在水平、竖直方向的偏差
     * @return {[verticalDiff,horizontalDiff]}
     * verticalDiff:竖直方向偏差，高于中心点为正，低于中心点为负
     * horizontalDiff：水平方向偏差，与初始偏差方向（左右）保持一致
     */
    public calculateDiff(): Array<number> {
        var v = new tpsClass.SurveyVector(this.sectionCenter, this.targetOriginal);
        var angle = this.levelAngle()
        let verticalDiff = 0;
        let horizontalDiff = 0;
        if (angle < Math.PI / 2 && angle > -Math.PI / 2) {
            verticalDiff = v.slope * Math.sin(angle);
            horizontalDiff = v.slope * Math.cos(angle);
            if(horizontalDiff < 0.01) {
                horizontalDiff = 0;
            }
        } else {
            verticalDiff = 0;
            horizontalDiff = 0;
        }
        return [horizontalDiff, verticalDiff];
    } 
    
    /** 
     * 获取全站仪（后视pb、前视pf）所在断面旋转之后与断面中心水平线的夹角
     * @return {number} 以弧度为单位的水平角
     */
    public levelAngle(): number {
        var v = new tpsClass.SurveyVector(this.sectionCenter, this.targetOriginal);
        var Angle = this.rotationAngle() + v.verticalAngle;
        return Angle;
    }
    
    /** 
     * 获取全站仪（后视pb、前视pf）所在断面与最初状态相比的旋转角
     * @return {number} 以弧度为单位的旋转角
     */
    public rotationAngle(): number {
        var vectorOriginal = new tpsClass.SurveyVector(this.targetOriginal, this.prismOriginal);
        var vectorNow = new tpsClass.SurveyVector(this.targetNow, this.prismNow);
        var Angle = vectorOriginal.verticalAngle - vectorNow.verticalAngle;
        return Angle;                      
    }

}
export { PrismSectionCalculate };